﻿namespace NEvoWeb.Modules.NB_Store
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web;

    public class GatewayWrapper
    {
        private Hashtable _objGateTable = new Hashtable();
        private string[] ProviderList;

        public void AutoResponse(int PortalID, HttpRequest Request)
        {
            OrderController controller = new OrderController();
            this.BuildGatewayArray(PortalID);
            string expression = Request.QueryString["ordID"];
            if (Versioned.IsNumeric(expression))
            {
                NB_Store_OrdersInfo order = controller.GetOrder(Conversions.ToInteger(expression));
                if (order != null)
                {
                    GatewayInterface interface2 = (GatewayInterface) this._objGateTable[order.GatewayProvider];
                    interface2.AutoResponse(PortalID, Request);
                }
            }
            else if (this._objGateTable.Count > 1)
            {
                SharedFunctions.UpdateLog("TO SUPPORT MULTIPLE GATEWAYS USE, '.../ordID/[ORDERID]/...' IN THE AUTORESPONSE URL");
            }
            else
            {
                ICollection keys = this._objGateTable.Keys;
                string[] array = new string[(this._objGateTable.Count - 1) + 1];
                keys.CopyTo(array, 0);
                ((GatewayInterface) this._objGateTable[array[0].ToString()]).AutoResponse(PortalID, Request);
            }
        }

        public void BuildGatewayArray(int PortalID)
        {
            if (this._objGateTable.Count == 0)
            {
                string expression = SharedFunctions.GetStoreSetting(PortalID, "gateway.provider", "XX");
                if (SharedFunctions.GetStoreSettingBoolean(PortalID, "debug.mode"))
                {
                    SharedFunctions.UpdateLog("ProviderClassList = " + expression);
                }
                this.ProviderList = Strings.Split(expression, ";", -1, CompareMethod.Binary);
                int upperBound = this.ProviderList.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    if (SharedFunctions.GetStoreSettingBoolean(PortalID, "debug.mode"))
                    {
                        SharedFunctions.UpdateLog("ProviderList" + i.ToString() + " = " + this.ProviderList[i]);
                    }
                    if (this.ProviderList[i] != "")
                    {
                        string[] strArray = Strings.Split(this.ProviderList[i], ",", -1, CompareMethod.Binary);
                        if (SharedFunctions.GetStoreSettingBoolean(PortalID, "debug.mode"))
                        {
                            SharedFunctions.UpdateLog("ProvKey = " + strArray[0]);
                        }
                        this._objGateTable.Add(strArray[0], GatewayInterface.Instance(strArray[0], strArray[1]));
                    }
                }
            }
        }

        public bool GetBankClick(int PortalID, HttpRequest Request)
        {
            this.BuildGatewayArray(PortalID);
            int upperBound = this.ProviderList.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (this.ProviderList[i] != "")
                {
                    string[] strArray = Strings.Split(this.ProviderList[i], ",", -1, CompareMethod.Binary);
                    GatewayInterface interface2 = (GatewayInterface) this._objGateTable[strArray[0]];
                    if (interface2.GetBankClick(PortalID, Request))
                    {
                        int orderID = CurrentCart.GetCurrentCart(PortalID).OrderID;
                        OrderController controller = new OrderController();
                        NB_Store_OrdersInfo order = controller.GetOrder(orderID);
                        if (order != null)
                        {
                            order.GatewayProvider = strArray[0];
                            controller.UpdateObjOrder(order);
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public string GetButtonHtml(int PortalID, int OrderID, string Lang)
        {
            string str3 = "";
            string str2 = HttpUtility.HtmlDecode(SharedFunctions.GetStoreSettingText(PortalID, "gatewaysep.template", SharedFunctions.GetCurrentCulture()));
            this.BuildGatewayArray(PortalID);
            int upperBound = this.ProviderList.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (this.ProviderList[i] != "")
                {
                    string[] strArray = Strings.Split(this.ProviderList[i], ",", -1, CompareMethod.Binary);
                    GatewayInterface interface2 = (GatewayInterface) this._objGateTable[strArray[0]];
                    str3 = str3 + interface2.GetButtonHtml(PortalID, OrderID, Lang) + str2;
                }
            }
            if (str3.Length > str2.Length)
            {
                str3 = str3.Substring(0, str3.Length - str2.Length);
            }
            return str3;
        }

        public string GetButtonHtml(int PortalID, int OrderID, string Lang, string GatewayRef)
        {
            this.BuildGatewayArray(PortalID);
            Hashtable availableGatewaysTable = SharedFunctions.GetAvailableGatewaysTable(PortalID);
            GatewayInterface interface2 = (GatewayInterface) this._objGateTable[((NB_Store_GatewayInfo) availableGatewaysTable[GatewayRef]).assembly];
            return interface2.GetButtonHtml(PortalID, OrderID, Lang);
        }

        public string GetCompletedHtml(int PortalID, int UserID, HttpRequest Request, int OrderID)
        {
            string str2 = "No Order found. OrderID=" + OrderID.ToString();
            OrderController controller = new OrderController();
            this.BuildGatewayArray(PortalID);
            NB_Store_OrdersInfo order = controller.GetOrder(OrderID);
            if (order != null)
            {
                str2 = ((GatewayInterface) this._objGateTable[order.GatewayProvider]).GetCompletedHtml(PortalID, UserID, Request);
            }
            return str2;
        }

        public void SetBankRemotePost(int PortalID, int OrderID, string Lang, HttpRequest Request)
        {
            OrderController controller = new OrderController();
            this.BuildGatewayArray(PortalID);
            NB_Store_OrdersInfo order = controller.GetOrder(OrderID);
            if (order != null)
            {
                ((GatewayInterface) this._objGateTable[order.GatewayProvider]).SetBankRemotePost(PortalID, OrderID, Lang, Request);
            }
        }
    }
}

